home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Macintosh Drag and Drop / Demo Applications / FinderDrag / Spaces.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-09  |  7.1 KB  |  280 lines  |  [TEXT/MPS ]

  1.  
  2.  
  3. #include <Errors.h>
  4. #include <QuickDraw.h>
  5. #include <Windows.h>
  6.  
  7. #include "FinderStuff.h"
  8. #include "FinderDrag.h"
  9. #include "Spaces.h"
  10.  
  11. #include <Icons.h>
  12.  
  13. // —————————————————————————————————————————————————————————————————————————
  14. // —————————————————————————————————————————————————————————————————————————
  15. // GetSpaceRect
  16.  
  17. void GetSpaceRect(WindowPtr win, short spaceIndex, Rect *rct)
  18. {
  19.     WindowDataHandle    winData;
  20.     
  21.     winData = (WindowDataHandle) GetWRefCon(win);
  22.     if (winData != NULL)
  23.         *rct = ((**winData).space)[spaceIndex].spaceRect;
  24. }
  25.  
  26.  
  27. // —————————————————————————————————————————————————————————————————————————
  28. // —————————————————————————————————————————————————————————————————————————
  29. // WhichSpace determines where the drop has taken place.  This app only
  30. // has two spaces, and after doing a compare it will return the space
  31. // number, or kNoSpace if it didn't land in a space.
  32.  
  33. short WhichSpace(WindowPtr win, Point localPt)
  34. {
  35.     short                count, theSpace;
  36.     WindowDataHandle    winData;
  37.     
  38.     winData = (WindowDataHandle) GetWRefCon(win);
  39.     theSpace = kNoSpace;
  40.     
  41.     if (winData != NULL) {
  42.         for (count = 0; count < kNumberOfSpaces; count ++) {
  43.             Rect rct;
  44.             
  45.             GetSpaceRect(win, count, &rct);
  46.             if (PtInRect(localPt, &rct))
  47.                 theSpace = count;
  48.         }
  49.     }
  50.  
  51.     return theSpace;
  52. }
  53.  
  54.  
  55. // —————————————————————————————————————————————————————————————————————————
  56. // —————————————————————————————————————————————————————————————————————————
  57. // SendSetterToSelf
  58.  
  59. OSErr SendSetterToSelf(short spaceNum, WindowPtr win, FSSpecPtr hfsObj)
  60. {
  61.     AppleEvent send;
  62.     AEDesc    selfTarget;
  63.     ProcessSerialNumber psn;
  64.     long    longSpaceNum;
  65.     OSErr    err;
  66.  
  67.     longSpaceNum = spaceNum;
  68.     selfTarget.dataHandle = NULL;
  69.     send.dataHandle = NULL;
  70.  
  71.     (void) GetCurrentProcess(&psn);
  72.     err = CreateAETarget(&psn, &selfTarget);
  73.     if (err != noErr) goto BailOut;
  74.     
  75.     err = MakeAppleEvent(kPrivateEventClass, kAESetSpace, 
  76.             &selfTarget, &send);
  77.     if (err != noErr) goto BailOut;
  78.     
  79.     err = AEPutParamPtr(&send, keyWindowOfSpace, typeLongInteger, (Ptr) &win, sizeof(WindowPtr));
  80.     if (err != noErr) goto BailOut;
  81.  
  82.     err = AEPutParamPtr(&send, keyNumOfSpace, typeLongInteger, (Ptr) &longSpaceNum, sizeof(long));
  83.     if (err != noErr) goto BailOut;
  84.  
  85.     err = AEPutParamPtr(&send, keySpecOfSpace, typeFSS, (Ptr) hfsObj, sizeof(FSSpec));
  86.     if (err != noErr) goto BailOut;
  87.  
  88.     err = SendAppleEvent(&send, NULL, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer);
  89.  
  90. BailOut:
  91.     if (selfTarget.dataHandle != NULL)    (void) AEDisposeDesc(&selfTarget);
  92.     if (send.dataHandle != NULL)        (void) AEDisposeDesc(&send);
  93.  
  94.     return err;
  95. }
  96.  
  97.  
  98. // —————————————————————————————————————————————————————————————————————————
  99. // —————————————————————————————————————————————————————————————————————————
  100. // SendWindowSpaceSetter
  101.  
  102. void SendWindowSpaceSetter(WindowPtr win, DragReference theDrag, FSSpecPtr hfsObj)
  103. {
  104.     Point                 lePoint;
  105.     WindowDataHandle    winData;
  106.     GrafPtr                oldPort;
  107.     short                spaceNum;
  108.     OSErr                err;
  109.  
  110.     winData = (WindowDataHandle) GetWRefCon(win);
  111.  
  112.     if (winData != NULL) {
  113.         GetPort(&oldPort);
  114.         SetPort(win);
  115.         GetDragMouse(theDrag, &lePoint, 0L);
  116.         GlobalToLocal(&lePoint);
  117.         spaceNum = WhichSpace(win, lePoint);
  118.         SetPort(oldPort);
  119.  
  120.         if (spaceNum != kNoSpace)
  121.             err = SendSetterToSelf(spaceNum, win, hfsObj);
  122.     }
  123.     else
  124.         Debugger();
  125. }
  126.  
  127.  
  128. // —————————————————————————————————————————————————————————————————————————
  129. // —————————————————————————————————————————————————————————————————————————
  130. // SetSpaceIndex
  131.  
  132. void SetSpaceIndex(WindowPtr win, short index, FSSpecPtr hfsObj)
  133. {
  134.     WindowDataHandle    winData;
  135.     GrafPtr                oldPort;
  136.     short                spaceNum;
  137.  
  138.     GetPort(&oldPort);
  139.     SetPort(win);
  140.     spaceNum = index;
  141.     winData = (WindowDataHandle) GetWRefCon(win);
  142.  
  143.     if (spaceNum != kNoSpace) {
  144.         //
  145.         // Drag was dropped in a valid space- set its
  146.         // iconSuite, FSSpec, and its isTaken field
  147.         //
  148.         SpacePtr    thisSpace;
  149.         OSErr        err;
  150.  
  151.         HLock((Handle) winData);
  152.         thisSpace = &(**winData).space[spaceNum];            
  153.  
  154.         if (thisSpace->spaceIsTaken == true) {
  155.             //
  156.             // But first we gotta get rid of the current occupant of
  157.             // this space..
  158.             //
  159.             Handle    iconSuite;
  160.  
  161.             iconSuite = thisSpace->spaceIconSuite;
  162.  
  163.             if (iconSuite != NULL)
  164.                 DisposeIconSuite(iconSuite, true);
  165.         }
  166.     
  167.         thisSpace->spaceIsTaken = true;
  168.         BlockMoveData(hfsObj, &thisSpace->spaceSpec, sizeof(FSSpec));
  169.         err = GetIconSuiteFromFinder(hfsObj, &thisSpace->spaceIconSuite);
  170.         HUnlock((Handle) winData);
  171.     }
  172.     else
  173.         Debugger();
  174.  
  175.     InvalRect(&win->portRect);
  176.     SetPort(oldPort);
  177. }
  178.  
  179.  
  180. // —————————————————————————————————————————————————————————————————————————
  181. // —————————————————————————————————————————————————————————————————————————
  182. // GetSpaceIconSuite
  183.  
  184. void GetSpaceIconSuite(WindowPtr win, short count, Handle *iconSuite)
  185. {
  186.     WindowDataHandle    winData;
  187.  
  188.     winData = (WindowDataHandle) GetWRefCon(win);
  189.     if (winData != NULL)
  190.         *iconSuite = (**winData).space[count].spaceIconSuite;
  191. }    
  192.  
  193.  
  194. // —————————————————————————————————————————————————————————————————————————
  195. // —————————————————————————————————————————————————————————————————————————
  196. // InitSpaces
  197.  
  198. void InitSpaces(WindowDataHandle winData)
  199. {
  200.     short index;
  201.     short rctTop, rctLeft;    
  202.     
  203.     rctTop = 15; rctLeft = 30;
  204.     for (index = 0; index < kNumberOfSpaces; index ++) {
  205.         SetRect(&((**winData).space)[index].spaceRect, rctLeft, rctTop, 
  206.                         rctLeft + kSpaceHSize, rctTop + kSpaceVSize);
  207.         ((**winData).space)[index].spaceIconSuite = NULL;
  208.         ((**winData).space)[index].spaceIsTaken = false;
  209.         ((**winData).space)[index].spaceIsSelected = false;
  210.     
  211.         rctTop += kSpaceVSize + 15;
  212.     }
  213. }
  214.  
  215.  
  216. // —————————————————————————————————————————————————————————————————————————
  217. // —————————————————————————————————————————————————————————————————————————
  218.  
  219. pascal OSErr DoAESetSpace(AppleEvent *ae, AppleEvent *reply, long refCon)
  220. {
  221. #pragma unused(reply, refCon)
  222.     OSErr            err;
  223.     DescType        returnType;
  224.     Size            returnSize;
  225.     long            whichSpace;
  226.     WindowPtr        whichWindow;
  227.     FSSpec            hfsObj;
  228.  
  229.     err = AEGetParamPtr(ae, keyWindowOfSpace, typeLongInteger, &returnType, 
  230.                         (Ptr) &whichWindow, sizeof(WindowPtr), &returnSize);
  231.     if (err != noErr)  goto BailOut;
  232.  
  233.     err = AEGetParamPtr(ae, keyNumOfSpace, typeLongInteger, &returnType,
  234.                         (Ptr) &whichSpace, sizeof(long), &returnSize);
  235.     if (err != noErr)  goto BailOut;
  236.  
  237.     err = AEGetParamPtr(ae, keySpecOfSpace, typeFSS, &returnType,
  238.                         (Ptr) &hfsObj, sizeof(FSSpec), &returnSize);
  239.     if (err != noErr)  goto BailOut;
  240.  
  241.     SetSpaceIndex(whichWindow, (short) whichSpace, &hfsObj);
  242.  
  243. BailOut:
  244.     return err;
  245. }
  246.  
  247.  
  248.  
  249. // —————————————————————————————————————————————————————————————————————————
  250. // —————————————————————————————————————————————————————————————————————————
  251. // GetSpaceSpec returns the FSSpec for the slot (or an error if there ain't
  252. // one.)
  253.  
  254. OSErr GetSpaceSpec(WindowPtr win, short spaceIndex, FSSpecPtr theSpec)
  255. {
  256.     WindowDataHandle    windowData;
  257.     SpacePtr            leSpace;
  258.     OSErr                err;
  259.     Boolean             foundIt;
  260.     
  261.     foundIt = false;
  262.     err = noErr;
  263.     windowData = (WindowDataHandle) GetWRefCon(win);
  264.     
  265.     if (windowData != NULL) {
  266.         leSpace = & (**windowData).space[spaceIndex];    
  267.  
  268.         if (leSpace->spaceIsTaken == true) {
  269.             foundIt = true;
  270.             BlockMoveData(&leSpace->spaceSpec, theSpec, sizeof(FSSpec));
  271.         }
  272.     }
  273.     
  274.     // If for some reason we didn't get a valid FSSpec, return fnfErr
  275.     if (foundIt == false)
  276.         err = fnfErr;
  277.  
  278.     return err;
  279. }
  280.